﻿using Microsoft.EntityFrameworkCore;
using PHMEE.EnergyEfficiency;
using PHMEE.Entities.DaqInfo.Ams;
using PHMEE.Entities.DaqInfo.TdDevices;
using PHMEE.Entities.Diagnosis;
using PHMEE.Entities.MqttDt.CollectPara;
using PHMEE.EntityFrameworkCore.Extensions;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace PHMEE.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class PHMEEDbContext :
    AbpDbContext<PHMEEDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    #region BaseInfo

    public DbSet<Company> Companies { get; set; }

    public DbSet<Fleet> Fleets { get; set; }

    public DbSet<Vessel> Vessels { get; set; }

    public DbSet<Unit> Units { get; set; }

    public DbSet<Equipment> Equipments { get; set; }

    public DbSet<Component> Components { get; set; }

    public DbSet<Point> Points { get; set; }

    public DbSet<MainenanceAdvice> MainenanceAdvices { get; set; }

    public DbSet<BearFactory> BearFactories { get; set; }

    public DbSet<BearLib> BearLibs { get; set; }

    #endregion

    #region DaqInfo
    // 阈值
    public DbSet<Threshold> Thresholds { get; set; }
    // 采集定义类型
    public DbSet<Category> Categories { get; set; }
    //波形指标间隔
    public DbSet<WaveInterval> waveIntervals { get; set; }

    public DbSet<TdDatabase> TdDatabases { get; set; }
    public DbSet<TdDeviceType> TdDeviceTypes { get; set; }
    public DbSet<TdDeviceAttribute> TdDeviceAttributes { get; set; }
    public DbSet<TdDeviceInstance> TdDeviceInstances { get; set; }
    public DbSet<TdDevicePointRelation> TdDevicePointRelations { get; set; }
    public DbSet<TdSqlScript> tdSqlScripts { get; set; }

    public DbSet<FlowmeterTemplate> flowmeterTemplates { get; set; }
    public DbSet<DigitalConfig> DigitalConfigs { get; set; }
    #endregion


    #region MqttDt
    //请求策略
    public DbSet<SysPara> SysParas { get; set; }
    public DbSet<SysParaExtend> SysParaExtendeds { get; set; }
    public DbSet<Sensor> Sensors { get; set; }
    public DbSet<VibChannelPara> VibChannels { get; set; }
    public DbSet<VibPara> VibParas { get; set; }
    public DbSet<TechChannelPara> TechChannelParas { get; set; }
    public DbSet<SpeedChannelPara> SpeedChannelParas { get; set; }
    public DbSet<TriggerPara> TriggerParas { get; set; }
    public DbSet<TriggerLimitPara> TriggerLimitParas { get; set; }
    #endregion

    #region Entities from the modules

    /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityDbContext and ITenantManagementDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    //Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }

    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

    #endregion

    #region Diagnosis

    public DbSet<AlarmRecord> AlarmRecords { get; set; }

    #endregion Diagnosis

    #region 能效 1.0 迁移

    #nullable enable

    public DbSet<VesselEeConfig> VesselEeConfigs { get; set; }

    public DbSet<CiiReductionFactor> CiiReductionFactors { get; set; }
    
    public DbSet<TimeSeriesDataLog> TimeSeriesDataLogs { get; set; }

    public DbSet<TimeSeriesTombstones> TimeSeriesTombstones { get; set; }

    public DbSet<OvdPort> OvdPorts { get; set; }
    
    #nullable disable

    #endregion 能效 1.0 迁移

    public PHMEEDbContext(DbContextOptions<PHMEEDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureFeatureManagement();
        builder.ConfigureTenantManagement();

        /* Configure your own tables/entities inside here */

        builder.ConfigureBaseInfo();

        builder.ConfigureDiagnosis();
    }
}
